package com.tjetc.controller;

import com.tjetc.common.JsonResult;
import com.tjetc.entity.Comment;
import com.tjetc.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /***
     * 用户添加评论
     * @param comment
     * @return
     */
    @RequestMapping("add")
    public JsonResult add(Comment comment){
        System.out.println(comment);
        int comment1 = commentService.addComment(comment);
        if(comment1<=0){
            return new JsonResult(1,"评论添加失败",comment1);
        }else{
            return new JsonResult(0,"评论添加成功",comment1);
        }
    }

    /***
     * 菜品id查询评论
     * @param dishId
     * @return
     */
    @RequestMapping("query/{dishId}")
    public JsonResult queryByDishId(@PathVariable int dishId){
        List<Comment> comments = commentService.selectByDishId(dishId);
        if(comments!=null){
            return new JsonResult(0,"查询成功",comments);
        }else {
            return new JsonResult(1,"查询失败",comments);
        }
    }

    /***
     * 用户id查询评论
     * @param userId
     * @return
     */
    @RequestMapping("query/{userId}")
    public JsonResult queryByUserId(@PathVariable int userId){
        List<Comment> comments = commentService.selectByUserId(userId);
        if(comments!=null){
            return new JsonResult(0,"查询成功",comments);
        }else {
            return new JsonResult(1,"查询失败",comments);
        }
    }

    /***
     * 评论id删除评论
     * @param id
     * @return
     */
    @RequestMapping("delete/{id}")
    public JsonResult delete(@PathVariable int id){
        int i = commentService.deleteById(id);
        if(i>=1){
            return new JsonResult(0,"查询成功",i);
        }else {
            return new JsonResult(1,"查询失败",i);
        }
    }

    /***
     * 商家id查询该店铺的评论
     * @param merchantId
     * @return
     */
    @RequestMapping("querybymerchantId/{merchantId}")
    public JsonResult queryByMerchantId(@PathVariable int merchantId){
        List<Comment> comments = commentService.queryByMerchantId(merchantId);
        System.out.println(comments);
        if(comments.size()>=1){
            return new JsonResult(0,"查询成功",comments);
        }else {
            return new JsonResult(1,"暂无评论",comments);
        }
    }
}
